﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinKeyBoard.UcControls
{
    public partial class UcComonKey : UIUserControl
    {
        public UcComonKey()
        {
            InitializeComponent();
        }

        #region 属性字段
        private string inputStr;
        /// <summary>
        /// 显示输入的字符串（去掉“{”“}”）
        /// </summary>
        private string InputStr
        {
            get { return inputStr; }
            set
            {
                inputStr = value;
                if (inputStr != null)
                {
                    if (inputStr.EndsWith("}"))
                    {
                        inputStr = inputStr.Substring(0, inputStr.Length - 1);
                    }
                    if (inputStr.StartsWith("{"))
                    {
                        inputStr = inputStr.Substring(1, inputStr.Length - 1);
                    }

                    if (!int.TryParse(inputStr, out int result))
                    {
                        inputStr = inputStr.Insert(inputStr.Length , " ");
                    }
                }
            }
        }
        #endregion

        public delegate void ShowInputTextDelegate(object obj);
        /// <summary>
        /// 显示输入值的委托
        /// </summary>
        public ShowInputTextDelegate showInputTextDelegate;

        /// <summary>
        /// 键盘输入的值
        /// </summary>
        /// <param name="obj"></param>
        public virtual void KeyInput(object obj)
        {
            string result = obj.ToString();

            InputStr = result;
            showInputTextDelegate?.Invoke(InputStr);

            SendKeys.Send(result);//如果调试，这里会被卡住
        }
    }
}
